<template>
  <div id="app" @click.stop="isLogOut = false">
    <div v-if="this.$store.getters.token" class="banner-title">
      <div class="banner-con">
        <div class="bner-icon" @click="toIndex">
        </div>
        <div class="icon-content" @click.stop="isLogOut = true">
          <span class="header-icon" />
          <span class="user-cl">{{ userName }}</span>
          <i class="el-icon-caret-bottom" />
          <ul v-if="isLogOut" class="ul-bto">
            <li @click="logOut"> <i class="el-icon-switch-button" /> 退出登录</li>
          </ul>
        </div>
      </div>
    </div>
    <transition>
      <router-view />
    </transition>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    const validateNewPassword = (rule, value, callback) => {
      if (value === this.ruleForm.curPassw) {
        callback(new Error('新密码不能与原密码相同!'))
      } else {
        callback()
      }
    }
    const validateConPasd = (rule, value, callback) => {
      if (value !== this.ruleForm.newPassw) {
        callback(new Error('与新密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      isLogOut: false,
      rules: {
        curPassw: [
          { required: true, message: '请输入当前密码', trigger: 'blur' }
        ],
        newPassw: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: validateNewPassword, trigger: 'blur' }
        ],
        conNewPassw: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: validateConPasd, trigger: 'blur' }
        ]
      },
      ruleForm: {
        curPassw: '',
        newPassw: '',
        conNewPassw: ''
      },
      userId: '',
      passwordType: 'password',
      passwordType1: 'password',
      passwordType2: 'password'
    }
  },
  computed: {
    userName() {
      return this.$store.getters.token
    },
    hasPlatForm() {
      let ishas = false
      this.$store.getters.permission_routes.forEach(item => {
        if (item.uri === 'platForm') {
          ishas = true
        }
      })
      return ishas
    }
  },
  created() {
    window.clearCatch = false
  },
  methods: {
    showPwd(val) {
      if (val === 1) {
        if (this.passwordType === 'password') {
          console.log(1)
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
      } else if (val === 2) {
        if (this.passwordType1 === 'password') {
          this.passwordType1 = ''
        } else {
          this.passwordType1 = 'password'
        }
      } else {
        if (this.passwordType2 === 'password') {
          this.passwordType2 = ''
        } else {
          this.passwordType2 = 'password'
        }
      }
    },
    cancel() {
      this.$refs.ruleForm.resetFields()
      this.dialogVisible = false
      this.ruleForm = {
        curPassw: '',
        newPassw: '',
        conNewPassw: ''
      }
      this.passwordType = 'password'
      this.passwordType1 = 'password'
      this.passwordType2 = 'password'
    },
    toIndex() {
      if (!this.hasPlatForm) {
        this.$router.push('/')
      }
    },
    async logOut() {
      this.$confirm('确定退出当前账号吗？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(valid => {
        this.logOutApi()
      }).catch(() => {
      })
    },
    async logOutApi() {
      await this.$store.dispatch('user/logout')
      await this.$store.dispatch('permission/clearPermission_routes')
      setTimeout(() => {
        location.reload()
        this.$router.push(`/login`)
      }, 300)
    }
  }
}
</script>
<style lang="scss" scoped>
.form-fz{
  ::v-deep .el-input--mini .el-input__icon {
    line-height: 32px;
    margin-right: 150px;
  }
  ::v-deep .el-input{
    width: 0;
  }
  ::v-deep .el-input--mini .el-input__inner{
    padding-right: 30px;
  }
  .showPwd{
    position: absolute;
    left: 180px;
    display: inline-block;
    float: right;
    top: 2px;
  }
}
  .banner-title {
    height: 64px;
    line-height: 64px;
    width: 100%;
    box-shadow: 0 4px 4px rgba(0, 12, 45, 0.3);
    position: relative;
    z-index: 101;
    .icon-class {
      display: inline-block;
      height: 200px;
      width: 200px;
      background: #ccc;
    }
    .iconfont{
      font-size: 13px;
    }
    .icon-content {
      width: 180px;
      height: 62px;
      position: absolute;
      z-index: 9;
      right: 0;
      top: 0;
      display: flex;
      align-items: center;
      justify-content: center;
      .user-cl {
        color: #FFFFFF;
        padding: 0 2px;
        font-size: 14px
      }
      .el-icon-caret-bottom {
        color: #fff;
        padding-left: 8px;
      }
      .ul-bto {
        position: absolute;
        z-index: 10;
        top: 60px;
        background: #fff;
        box-shadow: 0 2px 2px rgba(0, 12, 45, 0.3);
        font-size: 12px;
        border-radius: 3px;
        padding: 0 16px;
        cursor: pointer;
        &.active {
          background: #ccc;
        }
        li {
          height: 30px;
          line-height: 30px;
          border-radius: 3px;
        }
      }
      .header-icon {
        display: inline-block;
        background: url('./assets/img/avatar.png');
        background-repeat: no-repeat;
        background-size: 100% 100%;
        height: 32px;
        width: 32px;
        margin-right: 8px;
      }
    }
    .banner-con {
      .bner-icon {
        padding-top: 10px;
        padding-left: 30px;
        height: 100%;
        width: 270px;
        cursor: pointer;
      }
      background: url('./assets/img/logoBg.png') no-repeat;
      background-size: 100% 100%;
      height: 100%;
      width: 100%;
    }
  }
</style>
